require 'config/boot'
require 'config/environment'
require 'kconv'
require 'yaml'
require 'set'

env = ENV['RAILS_ENV'] || 'development'
db_path = ENV['db_config'] || 'config/database.yml'
midashi_list = ENV['list']
gogi_list = ENV['acceptations']
$KCODE = 'u'
task :register_dictionary => [:register_midashi_in_dictionary] do
  list = Array.new
  items = Set.new
  File.open(midashi_list) do |file|
    while line = file.gets
      unless line.chomp!.blank? 
        items << line
      else
        list << items.to_a
        items = Set.new
      end
    end
  end

  File.open(gogi_list) do |file|
    list.each_with_index do |items,index|
      line = file.gets
      elements = line.chomp!.split(Regexp.new('\t'))
      DirectionWord.find(:all,
        :include => :acceptations,
        :conditions => ["direction_words.text IN (#{['?'] * items.size * ','})"] + items).
        each { |direction_word|
        unless direction_word.acceptations.map{|acc| acc.text }.include? elements[0]
          direction_word.acceptations << Acceptation.new(:text => elements[0])
        end
      }
    end
  end
end

task :register_midashi_in_dictionary do
  set = Set.new
  File.open(midashi_list) do |file|
    while line = file.gets
      set << line unless line.chomp!.blank?
    end
  end
    
  set.each{|item| DirectionWord.find_or_create_by_text item }
end

task :encode_to_utf8 do
  Dir::glob("dict/original/**/*").each do |source_file|
    #read & encode
    file = open(source_file)
      text = file.read.kconv(Kconv::UTF8,  Kconv::SJIS)
    file.close
    #write
    file = File.open("dict/utf8/#{File.basename(source_file)}",'w')
      file.puts text
    file.close
  end
end